home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
- *
- * Copyright © Palm Computing 1998 -- All Rights Reserved
- *
- *
- **********************************************************************/
-
- #include <Pilot.h> // all the system toolbox headers
- #include <SysEvtMgr.h> // Needed for search for EvtSysEventAvail
- #include <FeatureMgr.h> // Needed to get the ROM version
- #include <ScrDriverNew.h> // to switch to grayscale mode
- #include "GrayRsc.h" // application resource defines
- #include "GrayEdit.h"
-
-
- /***********************************************************************
- * Global defines for this module
- **********************************************************************/
- #define version20 0x02000000 // PalmOS 2.0 version number
- #define version30 0x03000000 // PalmOS 2.0 version number
-
-
- /***********************************************************************
- * Global variables for this module
- **********************************************************************/
- DWord gRomVersion;
- Boolean gSupportsGrayAPIs;
- Boolean gHasGrayScreen;
-
-
-
- /***********************************************************************
- *
- * FUNCTION: RomVersionCompatible
- *
- * DESCRIPTION: Check that the ROM version meets your
- * minimum requirement. Warn if the app was switched to.
- *
- * PARAMETERS: requiredVersion - minimum rom version required
- * (see sysFtrNumROMVersion in SystemMgr.h
- * for format)
- * launchFlags - flags indicating how the application was
- * launched. A warning is displayed only if
- * these flags indicate that the app is
- * launched normally.
- *
- * RETURNED: zero if rom is compatible else an error code
- *
- ***********************************************************************/
- static Err RomVersionCompatible (DWord requiredVersion, Word launchFlags)
- {
- DWord romVersion;
-
-
- // See if we're on in minimum required version of the ROM or later.
- // The system records the version number in a feature. A feature is a
- // piece of information which can be looked up by a creator and feature
- // number.
- FtrGet(sysFtrCreator, sysFtrNumROMVersion, &romVersion);
- if (romVersion < requiredVersion)
- {
- // If the user launched the app from the launcher, explain
- // why the app shouldn't run. If the app was contacted for something
- // else, like it was asked to find a string by the system find, then
- // don't bother the user with a warning dialog. These flags tell how
- // the app was launched to decided if a warning should be displayed.
- if ((launchFlags & (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp)) ==
- (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp))
- {
- FrmAlert (RomIncompatibleAlert);
-
- // Pilot 1.0 will continuously relaunch this app unless we switch to
- // another safe one. The sysFileCDefaultApp is considered "safe".
- if (romVersion < 0x02000000)
- {
- Err err;
-
- AppLaunchWithCommand(sysFileCDefaultApp, sysAppLaunchCmdNormalLaunch, NULL);
- }
- }
-
- return (sysErrRomIncompatible);
- }
-
- return 0;
- }
-
- /***********************************************************************
- *
- * FUNCTION: StartApplication
- *
- * DESCRIPTION: This routine sets up the initial state of the application.
- * It opens the application's database and sets up global variables.
- *
- * PARAMETERS: None.
- *
- * RETURNED: true if error (database couldn't be created)
- *
- ***********************************************************************/
- static Boolean StartApplication(void)
- {
- DWord depth; // Apps needing B/W should use 1, grayscale apps use 2
- DWord supported;
-
- FtrGet(sysFtrCreator, sysFtrNumROMVersion, &gRomVersion);
-
- gHasGrayScreen=0; // be pessimistic. Pilots and PalmPilots actually can do gray,
- // but that requires hacking
-
- // temporary
- // gRomVersion=version20;
- // temporary
-
- if (gRomVersion>=version30) {
- gSupportsGrayAPIs=1; // we have APIs that support gray. Let's switch up if possible
-
- ScrDisplayMode(scrDisplayModeGetSupportedDepths, NULL, NULL, &supported, NULL);
- supported = supported >> 1; // we don't care about black-and-white support
- if (supported) { // if it supports any non-bw mode
- gHasGrayScreen=1;
-
- // switch to the lowest depth non-bw mode the screen supports
- depth = 2;
- while (!(supported & 0x01)) {
- depth << 1;
- supported = supported >> 1;
- }
- // should check the error return from this...
- ScrDisplayMode(scrDisplayModeSet, NULL, NULL, &depth, NULL);
- }
- } else
- gSupportsGrayAPIs=0;
-
- return false;
- }
-
- /***********************************************************************
- *
- * FUNCTION: StopApplication
- *
- * DESCRIPTION: This routine closes the application's database
- * and saves the current state of the application.
- *
- * PARAMETERS: nothing
- *
- * RETURNED: nothing
- *
- ***********************************************************************/
- static void StopApplication(void)
- {
- // Close all open forms to allow their frmCloseEvent handlers
- // to execute. An appStopEvent doesn't send frmCloseEvents.
- // FrmCloseAllForms will send all opened forms a frmCloseEvent.
- FrmCloseAllForms ();
-
- }
-
-
- /***********************************************************************
- *
- * FUNCTION: GetObjectPtr
- *
- * DESCRIPTION: This routine returns a pointer to an object in the active form.
- *
- * PARAMETERS: objectID - id of the object
- *
- * RETURNED: pointer to the object's data structure
- *
- ***********************************************************************/
- static VoidPtr GetObjectPtr(Int objectID)
- {
- FormPtr frm;
-
- frm = FrmGetActiveForm();
- return(FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, objectID)));
- }
-
- /***********************************************************************
- *
- * FUNCTION: MainViewInit
- *
- * DESCRIPTION: Initializes the main form and the list object.
- *
- * PARAMETERS: frm - pointer to the main form.
- *
- * RETURNED: Nothing.
- *
- ***********************************************************************/
- static void MainViewInit(void)
- {
- FormPtr frm;
-
-
- // Get a pointer to the main form.
- frm = FrmGetActiveForm();
-
- // Draw the form.
- FrmDrawForm(frm);
- }
-
- /***********************************************************************
- *
- * FUNCTION: PtInGadget
- *
- * RETURNED: True if the point is in the bounds of the gadget
- *
- ***********************************************************************/
- static Boolean PtInGadget(Word obj, SWord x, SWord y, FormPtr frm)
- {
- RectangleType r;
- FrmGetObjectBounds (frm, FrmGetObjectIndex (frm, obj), &r);
- return (RctPtInRectangle (x, y, &r));
- }
-
- /***********************************************************************
- *
- * FUNCTION: MainViewHandleEvent
- *
- * DESCRIPTION: Handles processing of events for the “main” form.
- *
- * PARAMETERS: event - the most recent event.
- *
- * RETURNED: True if the event is handled, false otherwise.
- *
- ***********************************************************************/
- static Boolean MainViewHandleEvent(EventPtr event)
- {
- Boolean handled = false;
- RectangleType r;
- FormPtr frm;
-
-
- switch (event->eType)
- {
- case ctlSelectEvent: // A control button was pressed and released.
- if (event->data.ctlEnter.controlID == 0) {
- handled = true;
- }
- break;
-
- case penDownEvent:
- case penMoveEvent:
- frm = FrmGetActiveForm ();
-
- if (PtInGadget(GrayMainPixelGadget, event->screenX, event->screenY, frm))
- HitPixel(event->screenX, event->screenY);
-
- else if (PtInGadget(GrayMainBigBWGadget, event->screenX, event->screenY, frm))
- SaveAndSwitch(GrayMainBigBWGadget);
-
- else if (PtInGadget(GrayMainBigGrayGadget, event->screenX, event->screenY, frm))
- SaveAndSwitch(GrayMainBigGrayGadget);
-
- else if (PtInGadget(GrayMainSmallBWGadget, event->screenX, event->screenY, frm))
- SaveAndSwitch(GrayMainSmallBWGadget);
-
- else if (PtInGadget(GrayMainSmallGrayGadget, event->screenX, event->screenY, frm))
- SaveAndSwitch(GrayMainSmallGrayGadget);
-
-
- else if (PtInGadget(GrayMainPen0Gadget, event->screenX, event->screenY, frm))
- SetPenTo(0);
- else if (PtInGadget(GrayMainPen1Gadget, event->screenX, event->screenY, frm))
- SetPenTo(1);
- else if (PtInGadget(GrayMainPen2Gadget, event->screenX, event->screenY, frm))
- SetPenTo(2);
- else if (PtInGadget(GrayMainPen3Gadget, event->screenX, event->screenY, frm))
- SetPenTo(3);
- break;
-
-
- case frmUpdateEvent:
- DrawEditorAndDisplays();
- break;
-
- case frmOpenEvent:
- MainViewInit();
- InitGrayEdit();
- DrawEditorAndDisplays();
- handled = true;
- break;
-
- }
- return(handled);
- }
-
-
- /***********************************************************************
- *
- * FUNCTION: ApplicationHandleEvent
- *
- * PARAMETERS: event - a pointer to an EventType structure
- *
- * RETURNED: True if the event has been handled and should not be
- * passed to a higher level handler.
- *
- ***********************************************************************/
- static Boolean ApplicationHandleEvent(EventPtr event)
- {
- FormPtr frm;
- Int formId;
- Boolean handled = false;
-
- if (event->eType == frmLoadEvent)
- {
- // Load the form resource specified in the event then activate the form.
- formId = event->data.frmLoad.formID;
- frm = FrmInitForm(formId);
- FrmSetActiveForm(frm);
-
- // Set the event handler for the form. The handler of the currently
- // active form is called by FrmDispatchEvent each time it receives an event.
- switch (formId)
- {
- case GrayMainForm:
- FrmSetEventHandler(frm, MainViewHandleEvent);
- break;
-
- }
- handled = true;
- }
-
- return handled;
- }
-
-
- /***********************************************************************
- *
- * FUNCTION: EventLoop
- *
- * DESCRIPTION: A simple loop that obtains events from the Event
- * Manager and passes them on to various applications and
- * system event handlers before passing them on to
- * FrmDispatchEvent for default processing.
- *
- * PARAMETERS: None.
- *
- * RETURNED: Nothing.
- *
- ***********************************************************************/
- static void EventLoop(void)
- {
- EventType event;
- Word error;
-
- do
- {
- // Get the next available event.
- EvtGetEvent(&event, evtWaitForever);
-
- // Give the system a chance to handle the event.
- if (! SysHandleEvent(&event))
-
- // Give the menu bar a chance to update and handle the event.
- if (! MenuHandleEvent(0, &event, &error))
-
- // Give the application a chance to handle the event.
- if (! ApplicationHandleEvent(&event))
-
- // Let the form object provide default handling of the event.
- FrmDispatchEvent(&event);
- }
- while (event.eType != appStopEvent);
- }
-
-
- /***********************************************************************
- *
- * FUNCTION: PilotMain
- *
- * DESCRIPTION: This function is the equivalent of a main() function
- * under standard “C”. It is called by the Emulator to begin
- * execution of this application.
- *
- * PARAMETERS: cmd - command specifying how to launch the application.
- * cmdPBP - parameter block for the command.
- * launchFlags - flags used to configure the launch.
- *
- * RETURNED: Any applicable error codes.
- *
- ***********************************************************************/
- DWord PilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
- {
- Word error;
-
- error = RomVersionCompatible (version20, launchFlags);
- if (error)
- return error;
-
- // Check for a normal launch.
- if (cmd == sysAppLaunchCmdNormalLaunch)
- {
- // Initialize the application's global variables and database.
- if (!StartApplication())
- {
- // Start the first form.
- FrmGotoForm(GrayMainForm);
-
- // Start the event loop.
- EventLoop();
-
- // Clean up before exiting the applcation.
- StopApplication();
- }
- }
-
- return 0;
- }
-